<?php

class ResizeImage {

    var $image;
    var $image_type;
    var $fileName;
    var $fixWidth;
    var $fixHeight;

    function __construct($fileName, $fixWidth, $fixHeight) {
	$this->fileName = $fileName;
	$this->fixWidth = $fixWidth;
	$this->fixHeight = $fixHeight;
    }


    function load() {
	$image_info = getimagesize($this->fileName);
	$this->image_type = $image_info[2];
	if ($this->image_type == IMAGETYPE_JPEG) {

	    $this->image = imagecreatefromjpeg($this->fileName);
	} elseif ($this->image_type == IMAGETYPE_GIF) {

	    $this->image = imagecreatefromgif($this->fileName);
	} elseif ($this->image_type == IMAGETYPE_PNG) {

	    $this->image = imagecreatefrompng($this->fileName);
	} elseif ($this->image_type == IMAGETYPE_BMP) {

	    $this->image = ImageCreateFromBMP($this->fileName);
	} 
    }

    function output($image_type = IMAGETYPE_JPEG) {
	$this->load();
	$this->resize();
	if ($image_type == IMAGETYPE_JPEG) {
	    imagejpeg($this->image);
	} elseif ($image_type == IMAGETYPE_GIF) {

	    imagegif($this->image);
	} elseif ($image_type == IMAGETYPE_PNG) {

	    imagepng($this->image);
	}
	imagedestroy($this->image);
    }

    function resize() {
	$srcWidth = imagesx($this->image);
	$srcHeight = imagesy($this->image);
	$arrSize = $this->getRatio($srcWidth, $srcHeight, $this->fixWidth, $this->fixHeight);
	$newWidth = $arrSize['width'];
	$newHeight = $arrSize['height'];
	$new_image = imagecreatetruecolor($newWidth, $newHeight);
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);
	$this->image = $new_image;
    }

    function getRatio($oldWidth, $oldHeight, $fixWidth, $fixHeight) {
	$distance = ($oldWidth - $oldHeight);
	$width = 0;
	$height = 0;

	if ($distance != 0) {
	    if ($distance < 0) {
		$distance *= -1;
	    }
	    $ratioW = $oldWidth * 1.0 / $distance;
	    $ratioH = $oldHeight * 1.0 / $distance;
	    if ($oldWidth > $fixWidth) {
		$width = $fixWidth;
	    } else {
		$width = $oldWidth;
	    }

	    $height = intval($width * ($ratioH / $ratioW));

	    if ($height > $fixHeight) {
		$height = $fixHeight;
		$width = intval($height * ($ratioW / $ratioH));
	    }
	} else {
	    $width = $fixWidth;
	    $height = $fixHeight;
	}
	return array('width' => $width, 'height' => $height);
    }

    /**
     * Reize and Crop with parameters: $x, $y, $width, $height
     * @param int $x
     * @param int $y
     * @param int $width
     * @param int $height
     * @param string $image_type
     */
    public function crop($x = 0, $y = 0, $width, $height, $image_type = IMAGETYPE_PNG) {
	$this->load();
	$this->resize();
	
	$minWidth = min(array($this->fixWidth, $width));
	$minHeight = min(array($this->fixHeight, $height));
	$dst_r = ImageCreateTrueColor($minWidth , $minHeight);

	imagecopyresampled($dst_r , $this->image, 0, 0, $x, $y, $minWidth , $minHeight, $minWidth , $minHeight);

	$this->image = $dst_r;

	if ($image_type == IMAGETYPE_JPEG) {
	    imagejpeg($this->image);
	} elseif ($image_type == IMAGETYPE_GIF) {

	    imagegif($this->image);
	} elseif ($image_type == IMAGETYPE_PNG) {

	    imagepng($this->image);
	} 
	
	imagedestroy($this->image);
    }

}

?>
